package net.maku.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import net.maku.framework.common.utils.PageResult;
import net.maku.framework.common.utils.Result;
import net.maku.convert.PreOrderConvert;
import net.maku.entity.PreOrderEntity;
import net.maku.service.PreOrderService;
import net.maku.query.PreOrderQuery;
import net.maku.vo.PreOrderVO;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import java.util.List;

/**
* 预购管理
*
* @author 小车 
* @since 1.0.0 2025-03-25
*/
@RestController
@RequestMapping("order/preOrder")
@Tag(name="预购管理")
@AllArgsConstructor
public class PreOrderController {
    private final PreOrderService preOrderService;

    @GetMapping("page")
    @Operation(summary = "分页")
    @PreAuthorize("hasAuthority('order:preOrder:page')")
    public Result<PageResult<PreOrderVO>> page(@ParameterObject @Valid PreOrderQuery query){
        PageResult<PreOrderVO> page = preOrderService.page(query);
        return Result.ok(page);
    }

    @GetMapping("{id}")
    @Operation(summary = "信息")
    @PreAuthorize("hasAuthority('order:preOrder:info')")
    public Result<PreOrderVO> get(@PathVariable("id") Long id){
        PreOrderEntity entity = preOrderService.getById(id);
        return Result.ok(PreOrderConvert.INSTANCE.convert(entity));
    }

    @PostMapping
    @Operation(summary = "保存")
    @PreAuthorize("hasAuthority('order:preOrder:save')")
    public Result<String> save(@RequestBody PreOrderVO vo){
        preOrderService.save(vo);

        return Result.ok();
    }

    @PutMapping
    @Operation(summary = "修改")
    @PreAuthorize("hasAuthority('order:preOrder:update')")
    public Result<String> update(@RequestBody @Valid PreOrderVO vo){
        preOrderService.update(vo);

        return Result.ok();
    }

    @DeleteMapping
    @Operation(summary = "删除")
    @PreAuthorize("hasAuthority('order:preOrder:delete')")
    public Result<String> delete(@RequestBody List<Long> idList){
        preOrderService.delete(idList);

        return Result.ok();
    }
}